using System;
using System.Collections.Generic;
using System.Text;
using Xyne.Templates.BattleEffects;
using Microsoft.Xna.Framework.Net;
using System.Xml;

namespace Xyne.Actions
{
    public enum ProjectileEffects
    {
        None,
        Fireball,
        Phoenix,
        Tsunami
    }

    public class ProjectileAction : Action
    {
        private float healthChange;
        public float BaseHealthChange
        {
            get { return healthChange; }
            set { healthChange = value; }
        }

        public float HealthChange
        {
            get { return healthChange * Level; }
        }

        private float speed;
        public float Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        private ProjectileEffects effect = ProjectileEffects.None;
        public ProjectileEffects Effect
        {
            get { return effect; }
            set { effect = value; }
        }

        public ProjectileAction()
        {
            Type = ActionTypes.Projectile;
        }

        public ProjectileAction(Action action) 
            : base(action)
        {
            Type = ActionTypes.Projectile;

            if (action is ProjectileAction)
            {
                ProjectileAction projectile = action as ProjectileAction;
                BaseHealthChange = projectile.BaseHealthChange;
                Speed = projectile.Speed;
                Effect = projectile.Effect;
            }
        }

        #region Serialization
        public override void Serialize(PacketWriter writer)
        {
            base.SerializeAction(writer);

            writer.Write(BaseHealthChange);
            writer.Write(Speed);
            writer.Write((ulong)Effect);
        }

        public static ProjectileAction Deserialize(PacketReader reader)
        {
            ProjectileAction action = new ProjectileAction();
            action.DeserializeAction(reader);
            action.BaseHealthChange = reader.ReadSingle();
            action.Speed = reader.ReadSingle();
            action.Effect = (ProjectileEffects)reader.ReadUInt64();
            return action;
        }

        public override void WriteXml(XmlWriter writer)
        {
            base.WriteXml(writer);

            writer.WriteElementString("Effect", effect.ToString());
            writer.WriteElementString("HealthChange", healthChange.ToString());
            writer.WriteElementString("Speed", speed.ToString());
        }

        public override void ReadNode(XmlReader reader)
        {
            if (reader.LocalName == "Effect")
            {
                effect = (ProjectileEffects)Enum.Parse(typeof(ProjectileEffects), reader.ReadString(),true);
            }
            else if (reader.LocalName == "HealthChange")
            {
                healthChange = float.Parse(reader.ReadString());
            }
            else if (reader.LocalName == "Speed")
            {
                speed = float.Parse(reader.ReadString());
            }
        }
        #endregion
    }
}
